﻿Option Explicit On
Imports Microsoft.VisualBasic
Imports System
Imports System.Globalization

Partial Public Class User_IPORT
    Inherits System.Web.UI.Page
    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "USER MANAGEMENT"
            LoadData("", "0")
            clsGet.byddlfromDataSet(ddlRole, clsGet.LoadRole(), "Role_name", "Role_no")
            If (Request.QueryString("P1") & "" = "Y") Then
                Session("EditMaster") = "Y"
            Else
                Session("EditMaster") = Nothing
            End If
            If (Request.QueryString("P2") & "" <> "Y") Then
                btnAdd.Visible = False
            End If
            If (Request.QueryString("P3") & "" <> "Y") Then
                gridItem.Columns(4).Visible = False
            End If
        End If
    End Sub

    Private Sub LoadData(ByVal username As String, ByVal Role As String)
        Dim clsGet As New ClassFunctionGetData
        ViewState("User_IPORT") = SetDataSetForm(clsGet.GetUserIPORT(username, False, Role))
        gridItem.PageIndex = 0
        gridItem.DataSource = ViewState("User_IPORT")
        gridItem.DataBind()
    End Sub
    Private Function SetDataSetForm(ByVal ds As DataSet) As DataSet
        Dim userData As DataSet = CreateDataset()
        If Not (ds Is Nothing) Then
            If (ds.Tables(0).Rows(0).Item("User_name") & "" <> "") Then
                Dim user_name As String = ""
                Dim drN As DataRow
                For Each dr As DataRow In ds.Tables(0).Rows
                    If (dr.Item("User_name") & "" <> "") Then
                        If (user_name = dr.Item("User_name").ToString()) Then
                            If (userData.Tables(0).Rows.Count = 1) Then
                                userData.Tables(0).Rows(0).Item("RoleName") = userData.Tables(0).Rows(0).Item("RoleName").ToString() & "/" & dr.Item("Role_name").ToString()
                            Else
                                drN.Item("RoleName") = drN.Item("RoleName") & "/" & dr.Item("Role_name").ToString()
                            End If
                        Else
                            user_name = dr.Item("User_name").ToString()
                            If (userData.Tables(0).Rows(0).Item("User_name") = "") Then
                                userData.Tables(0).Rows(0).Item("User_name") = dr.Item("User_name").ToString()
                                userData.Tables(0).Rows(0).Item("Name") = dr.Item("Name").ToString()
                                userData.Tables(0).Rows(0).Item("RoleName") = dr.Item("Role_name").ToString()
                                userData.Tables(0).Rows(0).Item("Status") = dr.Item("Status").ToString()
                                userData.Tables(0).Rows(0).Item("Email") = dr.Item("Email").ToString()
                                userData.Tables(0).Rows(0).Item("Password") = dr.Item("Password").ToString()
                                userData.Tables(0).Rows(0).Item("Customer_no") = dr.Item("Customer_no").ToString()
                                userData.Tables(0).Rows(0).Item("Customer_name") = dr.Item("Customer_name").ToString()
                                userData.Tables(0).AcceptChanges()
                            Else
                                drN = userData.Tables(0).NewRow
                                drN.Item("User_name") = dr.Item("User_name").ToString()
                                drN.Item("Name") = dr.Item("Name").ToString()
                                drN.Item("RoleName") = dr.Item("Role_name").ToString()
                                drN.Item("Status") = dr.Item("Status").ToString()
                                drN.Item("Email") = dr.Item("Email").ToString()
                                drN.Item("Password") = dr.Item("Password").ToString()
                                drN.Item("Customer_no") = dr.Item("Customer_no").ToString()
                                drN.Item("Customer_name") = dr.Item("Customer_name").ToString()
                                userData.Tables(0).Rows.Add(drN)
                                userData.Tables(0).AcceptChanges()
                            End If
                        End If
                    End If
                Next
                userData.Tables(0).AcceptChanges()
            End If
        End If
        Return userData
    End Function
    Public Function CreateDataset() As DataSet
        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim dr As DataRow
        ds.Tables.Add(dt)
        dt.Columns.Add("name", GetType(String))
        dt.Columns.Add("RoleName", GetType(String))
        dt.Columns.Add("Status", GetType(String))
        dt.Columns.Add("User_name", GetType(String))
        dt.Columns.Add("Email", GetType(String))
        dt.Columns.Add("Password", GetType(String))
        dt.Columns.Add("Customer_no", GetType(String))
        dt.Columns.Add("Customer_name", GetType(String))
        ds.Tables(0).AcceptChanges()
        dr = ds.Tables(0).NewRow
        dr("name") = ""
        dr("RoleName") = ""
        dr("Status") = ""
        dr("User_name") = ""
        dr("Email") = ""
        dr("Password") = ""
        dr("Customer_no") = ""
        dr("Customer_name") = ""
        ds.Tables(0).Rows.Add(dr)

        Return ds
    End Function

    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridItem.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridItem.PageIndex = ddlPage.SelectedIndex
        gridItem.DataSource = ViewState("User_IPORT")
        gridItem.DataBind()
    End Sub

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridItem.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridItem.PageIndex = 0
            Case "prev"
                If (gridItem.PageIndex <> 0) Then
                    gridItem.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridItem.PageIndex <> gridItem.PageCount) Then
                    gridItem.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridItem.PageIndex = gridItem.PageCount
        End Select
        gridItem.DataSource = ViewState("User_IPORT")
        gridItem.DataBind()
    End Sub

    Private Sub gridItem_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridItem.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridItem.PageIndex <> 0) Then
            Index = Index Mod (gridItem.PageIndex * 10)
        End If
        If e.CommandName = "cmdedit" Then
            Session("User_IPORT") = gridItem.DataKeys(Index).Values("User_name") & "$" & gridItem.Rows(Index).Cells(0).Text & "$" & gridItem.DataKeys(Index).Values("Email") & "$" & gridItem.DataKeys(Index).Values("Status") & "$" & gridItem.DataKeys(Index).Values("Password") & "$" & gridItem.DataKeys(Index).Values("Customer_no") & "$" & gridItem.DataKeys(Index).Values("Customer_name")
            ModalPopupExtender1.Show()
            ' ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('Edit " & Index.ToString() & " !!');", True)
        ElseIf e.CommandName = "cmddelete" Then
            Dim user_name As String = gridItem.DataKeys(Index).Values("User_name")
            Dim conn As New System.Data.SqlClient.SqlConnection(connString)
            Dim clsDB As New ClassDatabase
            Dim clsFc As New ClassFunction
            Dim condition As Boolean = True
            Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
            Application.Lock()
            conn.Open()
            tran = clsDB.BeginTran(conn)
            If (clsFc.DeleteUserIPORT(user_name, conn, tran) > 0) Then
                If (clsFc.DeleteUser(user_name, conn, tran) < 1) Then
                    condition = False
                End If
            Else
                condition = False
            End If
            If (condition) Then
                clsDB.CommitTran(tran)
                LoadData("", "0")
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ลบข้อมูลเรียบร้อย!!');", True)
            Else
                clsDB.RollBackTran(tran)
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการลบข้อมูล !!');", True)
            End If
            conn.Close()
            Application.UnLock()
            conn.Dispose()
            ' ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('Delete " & Index.ToString() & " !!');", True)
        End If
    End Sub

    Private Sub gridItem_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridItem.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            If (gridItem.DataKeys(e.Row.RowIndex).Values("Status") & "" = "Y") Then
                e.Row.Cells(2).Text = "ใช้งาน"
            ElseIf (gridItem.DataKeys(e.Row.RowIndex).Values("Status") & "" = "N") Then
                e.Row.Cells(2).Text = "ปิดการใช้งาน"
            Else
                e.Row.Cells(2).Text = ""
            End If
        ElseIf (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridItem.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridItem.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridItem.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                Next
            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridItem.PageCount.ToString()
            End If
        End If
    End Sub

    Private Sub btnHide_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHide.Click
        LoadData("", "0")
    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        LoadData(txtName.Text, ddlRole.SelectedValue)
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub
End Class